﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SGP;
using System.Xml.Linq;

namespace AnimationEditor
{
    public partial class Form1 : Form
    {
        string animationPath;

        CSGP_Direct3D DX = CSGP_Direct3D.GetInstance();
        CSGP_TextureManager TM = CSGP_TextureManager.GetInstance();

        int imageID;

        Dictionary<string, Animation> anims = new Dictionary<string, Animation>();

        bool drawRendRect = true;
        bool drawColRect = false;
        bool drawActiveRect = false;
        bool placeAnchorPoint = false;
        bool placeWeaponOffset = false;

        bool looping = true;

        Bitmap bitSize;

        int rendX = 0;
        int rendY = 0;
        int rendWidth = 0;
        int rendHeight = 0;

        int colX = 0;
        int colY = 0;
        int colWidth = 0;
        int colHeight = 0;

        int actX = 0;
        int actY = 0;
        int actWidth = 0;
        int actHeight = 0;

        Point initialMousePosition = new Point(0, 0);
        Point currMousePosition = new Point(0, 0);

        bool ancXChange = true;
        bool ancYChange = true;
        bool wepXOffset = true;
        bool wepYOffset = true;

        bool durationChange = true;
        bool triggerChange = true;

        bool isPlaying = false;
        bool isLooping = false;

        float timeOnFrame = 0.0f;

        double elapsedTime = 0.0f;

        //opdlg.InitialDirectory = System.IO.Path.GetFullPath(System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\..\\Tacwars\\resource\\Images");

        //string fullPath = "../../Images/";
        string fullPath = System.IO.Path.GetFullPath(System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\..\\..\\resource\\Graphics\\");

        DateTime then;
        public bool Looping
        {
            get { return looping; }
            set { looping = value; }
        }

        public Form1()
        {
            //C:\Users\fullsail\Desktop\New folder (6)\megalands\MegaLands\Animation Editor\AnimationEditor\Images\
            InitializeComponent();

            DX.Initialize(SpriteSheet, false);
            DX.Initialize(PreviewPanel, false);

            DX.AddRenderTarget(SpriteSheet);
            DX.AddRenderTarget(PreviewPanel);

            TM.Initialize(DX.Device, DX.Sprite);

            animationPath = "AnchorPointTest.png";


            imageID = TM.LoadTexture(fullPath + animationPath);
            //animationPath = "../../Images/AnchorPointTest.png";
            //animationPath = "AnchorPointTest.png";
            //imageID = TM.LoadTexture("TheRoom.png");
            bitSize = new Bitmap(fullPath + animationPath);
            Graphics g = SpriteSheet.CreateGraphics();
            bitSize.SetResolution(g.DpiX, g.DpiY);
            g.Dispose();

            SpriteSheet.AutoScrollMinSize = bitSize.Size;

            RectInfoComboBox.Items.Add("RenderRect");
            RectInfoComboBox.Items.Add("CollisionRect");
            RectInfoComboBox.Items.Add("ActiveRect");
            RectInfoComboBox.SelectedIndex = 0;

            then = DateTime.Now;
        }

        public void Render()
        {
            DX.Clear(SpriteSheet, Color.DarkGray);

            DX.DeviceBegin();
            DX.SpriteBegin();

            //Render the Sprite Sheet panel:
            TM.Draw(imageID, 0 + SpriteSheet.AutoScrollPosition.X, 0 + SpriteSheet.AutoScrollPosition.Y);

            //Sprite Sheet panel autoscroll offset
            int offsetX = SpriteSheet.AutoScrollPosition.X;
            int offsetY = SpriteSheet.AutoScrollPosition.Y;

            //Draw a blue rectangle for the renderRect
            DX.DrawHollowRect(new Rectangle(rendX + offsetX, rendY + offsetY, rendWidth, rendHeight), Color.Blue, 1);
            //Draw a yellow rectangle for the collisionRect
            DX.DrawHollowRect(new Rectangle(colX + offsetX, colY + offsetY, colWidth, colHeight), Color.Yellow, 1);
            //Draw a red rectangle for the activeRect
            DX.DrawHollowRect(new Rectangle(actX + offsetX, actY + offsetY, actWidth, actHeight), Color.Red, 1);

            //Check to make sure there is a frame that can be rendered
            if (AnimationsListBox.SelectedIndex >= 0 && FrameListBox.SelectedIndex >= 0 && anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count >= 1 &&
                FrameListBox.SelectedIndex < anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count)
            {
                //Draw a green 5 x 5 dot for the anchor point
                DX.DrawRect(new Rectangle(anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].AnchorPointX - 2 + offsetX,
                    anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].AnchorPointY - 2 + offsetY, 5, 5), Color.Green);
                //Draw a yellow 5 x 5 dot for the weapon offset point
                DX.DrawRect(new Rectangle(anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].WeaponOffsetX - 2 + offsetX,
                    anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].WeaponOffsetY - 2 + offsetY, 5, 5), Color.Yellow);
            }

            //end rendering
            DX.SpriteEnd();
            DX.DeviceEnd();

            //Present the render information to the screen
            DX.Present();

            DX.Clear(PreviewPanel, Color.Black);

            DX.DeviceBegin();
            DX.SpriteBegin();

            //Render the Preview panel

            //check if there is a valid frame selected
            if (AnimationsListBox.SelectedIndex >= 0 && FrameListBox.SelectedIndex >= 0 && anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count >= 1 &&
                FrameListBox.SelectedIndex < anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count)
            {
                Point p = new Point(anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].AnchorPointX - anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].RendX,
                    anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].AnchorPointY - anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].RendY);
                //- (anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].RendWidth - p.X
                // - (anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].RendHeight - p.Y

                Point panelPoint = new Point(PreviewPanel.Width / 2, PreviewPanel.Height / 2);

                TM.Draw(imageID, panelPoint.X - p.X, panelPoint.Y - p.Y, 1, 1, new Rectangle(anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].RendX,
                    anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].RendY,
                    anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].RendWidth,
                    anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].RendHeight));

                DX.DrawRect(new Rectangle(PreviewPanel.Width / 2, PreviewPanel.Height / 2, 1, 1), Color.Red);

            }

            DX.SpriteEnd();
            DX.DeviceEnd();

            DX.Present();


        }

        public new void Update()
        {
            if (isPlaying)
            {
                PlayingCheckBox.Checked = true;
            }
            else
            {
                PlayingCheckBox.Checked = false;
                LoopingCheckBox.Checked = false;
            }

            if (isLooping)
            {
                LoopingCheckBox.Checked = true;
            }
            else
            {
                LoopingCheckBox.Checked = false;
            }


            ButtonColor();

            //time since last frame
            DateTime now = DateTime.Now;
            elapsedTime = (now - then).TotalSeconds;
            then = now;

            //if there is a frame selected to be updated
            if (AnimationsListBox.SelectedIndex >= 0 && FrameListBox.SelectedIndex >= 0 && anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count >= 1 &&
                FrameListBox.SelectedIndex < anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count)
            {
                //set the render rectangle info to the current frame's render rect info.
                rendX = anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].RendX;
                rendY = anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].RendY;
                rendWidth = anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].RendWidth;
                rendHeight = anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].RendHeight;

                //set the collision rectangle info to the current frame's collision rect info.
                colX = anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ColX;
                colY = anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ColY;
                colWidth = anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ColWidth;
                colHeight = anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ColHeight;

                //set the active rectangle info to the current frame's active rect info.
                actX = anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ActiveX;
                actY = anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ActiveY;
                actWidth = anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ActiveWidth;
                actHeight = anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ActiveHeight;

                //set the anchor Point info to the current frame's anchorPoint info.
                AnchorPointXNumericUpDown.Value = anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].AnchorPointX;
                AnchorPointYNumericUpDown.Value = anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].AnchorPointY;

                //set the weapon offset info to the current frame's weapon offset info.
                WeaponOffsetXNumericUpDown.Value = anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].WeaponOffsetX;
                WeaponOffsetYNumericUpDown.Value = anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].WeaponOffsetY;

                //set the frame duration info to the current frame's frame duration info.
                DurationNumericUpDown.Value = (decimal)anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].Duration;

                //set the frame trigger info to the current frame's frame trigger info;
                TriggerTextBox.Text = anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].Trigger.ToString();

                //Check the currently selected rectangle info
                //render rect info
                if (RectInfoComboBox.SelectedIndex == 0)
                {
                    RectXNumericUpDown.Value = rendX;
                    RectYNumericUpDown.Value = rendY;
                    RectWidthNumericUpDown.Value = rendWidth;
                    RectHeightNumericUpDown.Value = rendHeight;
                }
                //collision rect info
                else if (RectInfoComboBox.SelectedIndex == 1)
                {
                    RectXNumericUpDown.Value = colX;
                    RectYNumericUpDown.Value = colY;
                    RectWidthNumericUpDown.Value = colWidth;
                    RectHeightNumericUpDown.Value = colHeight;
                }
                //active rect info
                else if (RectInfoComboBox.SelectedIndex == 2)
                {
                    RectXNumericUpDown.Value = actX;
                    RectYNumericUpDown.Value = actY;
                    RectWidthNumericUpDown.Value = actWidth;
                    RectHeightNumericUpDown.Value = actHeight;
                }
            }
            else
            {
                RectXNumericUpDown.Value = (decimal)0;
                RectYNumericUpDown.Value = (decimal)0;
                RectWidthNumericUpDown.Value = (decimal)0;
                RectHeightNumericUpDown.Value = (decimal)0;

                AnchorPointXNumericUpDown.Value = (decimal)0;
                AnchorPointYNumericUpDown.Value = (decimal)0;

                WeaponOffsetXNumericUpDown.Value = (decimal)0;
                WeaponOffsetYNumericUpDown.Value = (decimal)0;

                DurationNumericUpDown.Value = (decimal)0;

                TriggerTextBox.Text = "";
            }
            //Update the Preview
            if (isPlaying && AnimationsListBox.SelectedIndex >= 0 && FrameListBox.SelectedIndex >= 0)
            {
                //increment the time on the current frame by the time since last frame
                timeOnFrame += (float)elapsedTime;

                //check if the animation is playing time on the current frame is longer than the frame's duration
                if (timeOnFrame >= anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].Duration)
                {
                    //the current frame is not the last frame
                    if (FrameListBox.SelectedIndex != FrameListBox.Items.Count - 1)
                    {
                        FrameListBox.SelectedIndex = FrameListBox.SelectedIndex + 1;
                        timeOnFrame = 0.0f;
                    }
                    //preview is on the last frame and looping so set current frame to first frame
                    else if (isLooping)
                    {
                        FrameListBox.SelectedIndex = 0;
                        timeOnFrame = 0.0f;
                    }
                    //all the frames have been rendered
                    else
                        isPlaying = false;

                }
            }

        }

        public void StopPlaying()
        {
            isPlaying = isLooping = false;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_FormClosing_1(object sender, FormClosingEventArgs e)
        {
            looping = false;
        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void numericUpDown4_ValueChanged(object sender, EventArgs e)
        {

            if (FrameListBox.Items.Count > 0 && AnimationsListBox.SelectedIndex >= 0 && FrameListBox.SelectedIndex >= 0 &&
                            anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count >= 0 &&
                            anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count >= FrameListBox.SelectedIndex + 1)
            {
                if (RectInfoComboBox.SelectedIndex == 0)
                {
                    rendY = (int)RectYNumericUpDown.Value;
                    anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].RendY = rendY;
                }
                else if (RectInfoComboBox.SelectedIndex == 1)
                {
                    colY = (int)RectYNumericUpDown.Value;
                    anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ColY = colY;
                }
                else if (RectInfoComboBox.SelectedIndex == 2)
                {
                    actY = (int)RectYNumericUpDown.Value;
                    anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ActiveY = actY;
                }
            }
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {

            if (FrameListBox.Items.Count > 0 && AnimationsListBox.SelectedIndex >= 0 && FrameListBox.SelectedIndex >= 0 &&
                anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count >= 0 &&
                anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count >= FrameListBox.SelectedIndex + 1)
            {
                if (RectInfoComboBox.SelectedIndex == 0)
                {
                    rendX = (int)RectXNumericUpDown.Value;
                    anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].RendX = rendX;
                }
                else if (RectInfoComboBox.SelectedIndex == 1)
                {
                    colX = (int)RectXNumericUpDown.Value;
                    anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ColX = colX;
                }
                else if (RectInfoComboBox.SelectedIndex == 2)
                {
                    actX = (int)RectXNumericUpDown.Value;
                    anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ActiveX = actX;
                }
            }
            //else
            //  RectXNumericUpDown.Value = 0;
        }

        private void numericUpDown3_ValueChanged(object sender, EventArgs e)
        {

            if (FrameListBox.Items.Count > 0 && AnimationsListBox.SelectedIndex >= 0 && FrameListBox.SelectedIndex >= 0 &&
                            anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count >= 0 &&
                            anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count >= FrameListBox.SelectedIndex + 1)
            {
                if (RectInfoComboBox.SelectedIndex == 0)
                {
                    rendWidth = (int)RectWidthNumericUpDown.Value;
                    anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].RendWidth = rendWidth;
                }
                else if (RectInfoComboBox.SelectedIndex == 1)
                {
                    colWidth = (int)RectWidthNumericUpDown.Value;
                    anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ColWidth = colWidth;
                }
                else if (RectInfoComboBox.SelectedIndex == 2)
                {
                    actWidth = (int)RectWidthNumericUpDown.Value;
                    anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ActiveWidth = actWidth;
                }
            }
        }

        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {

            if (FrameListBox.Items.Count > 0 && AnimationsListBox.SelectedIndex >= 0 && FrameListBox.SelectedIndex >= 0 &&
                            anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count >= 0 &&
                            anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count >= FrameListBox.SelectedIndex + 1)
            {
                if (RectInfoComboBox.SelectedIndex == 0)
                {
                    rendHeight = (int)RectHeightNumericUpDown.Value;
                    anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].RendHeight = rendHeight;
                }
                else if (RectInfoComboBox.SelectedIndex == 1)
                {
                    colHeight = (int)RectHeightNumericUpDown.Value;
                    anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ColHeight = colHeight;
                }
                else if (RectInfoComboBox.SelectedIndex == 2)
                {
                    actHeight = (int)RectHeightNumericUpDown.Value;
                    anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ActiveHeight = actHeight;
                }
            }
        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void viewToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void button12_Click(object sender, EventArgs e)
        {
            if (!isLooping)
            {
                isPlaying = isLooping = true;
            }
            else
                isPlaying = isLooping = false;
        }

        private void RenderRectButton_Click(object sender, EventArgs e)
        {
            RectInfoComboBox.SelectedIndex = 0;
            drawRendRect = true;
            drawColRect = false;
            drawActiveRect = false;
            placeAnchorPoint = false;
            placeWeaponOffset = false;
        }

        private void CollisionRectButton_Click(object sender, EventArgs e)
        {
            RectInfoComboBox.SelectedIndex = 1;
            drawRendRect = false;
            drawColRect = true;
            drawActiveRect = false;
            placeAnchorPoint = false;
            placeWeaponOffset = false;
        }

        private void ActiveRectButton_Click(object sender, EventArgs e)
        {
            RectInfoComboBox.SelectedIndex = 2;
            drawRendRect = false;
            drawColRect = false;
            drawActiveRect = true;
            placeAnchorPoint = false;
            placeWeaponOffset = false;
        }

        private void AnchorPointButton_Click(object sender, EventArgs e)
        {
            drawRendRect = false;
            drawColRect = false;
            drawActiveRect = false;
            placeAnchorPoint = true;
            placeWeaponOffset = false;
        }

        private void WeaponOffsetButton_Click(object sender, EventArgs e)
        {
            drawRendRect = false;
            drawColRect = false;
            drawActiveRect = false;
            placeAnchorPoint = false;
            placeWeaponOffset = true;
        }

        private void PlayPreviewButton_Click(object sender, EventArgs e)
        {
            //check if there is a valid frame selected
            if (AnimationsListBox.SelectedIndex >= 0 && FrameListBox.SelectedIndex >= 0 && anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count >= 1 &&
                FrameListBox.SelectedIndex < anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count)
            {
                if (!isPlaying)
                {
                    FrameListBox.SelectedIndex = 0;
                    isPlaying = true;
                }
                else
                    isPlaying = false;
            }
        }

        private void StopPreviewButton_Click(object sender, EventArgs e)
        {
            //check if there is a valid frame selected
            if (AnimationsListBox.SelectedIndex >= 0 && FrameListBox.SelectedIndex >= 0 && anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count >= 1 &&
                FrameListBox.SelectedIndex < anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count)
            {
                FrameListBox.SelectedIndex = 0;
                isPlaying = isLooping = false;
            }//blahok
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            //lDialogResult dr = ofd.ShowDialog();

            //opdlg.InitialDirectory = System.IO.Path.GetFullPath(System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\..\\Tacwars\\resource\\Images");
            ofd.InitialDirectory = System.IO.Path.GetFullPath(System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\images");

            if (DialogResult.OK == ofd.ShowDialog())
            {
                imageID = TM.LoadTexture(ofd.FileName);
                animationPath = ofd.FileName;//.Split(@"\/", StringSplitOptions.RemoveEmptyEntries);

                animationPath = animationPath.Substring(animationPath.LastIndexOf('\\') + 1);
                //imageID = TM.LoadTexture("TheRoom.png");
                bitSize = new Bitmap(ofd.FileName);
                Graphics g = SpriteSheet.CreateGraphics();
                bitSize.SetResolution(g.DpiX, g.DpiY);
                g.Dispose();

                SpriteSheet.AutoScrollMinSize = bitSize.Size;

                DX.Resize(SpriteSheet, SpriteSheet.ClientSize.Width, SpriteSheet.ClientSize.Height, true);
            }
        }

        private void NewAnimationButton_Click(object sender, EventArgs e)
        {

            if (AnimationNameTextBox.Text != "")
            {
                if (!anims.ContainsKey(AnimationNameTextBox.Text))
                {
                    Animation newAnim = new Animation();
                    newAnim.Name = AnimationNameTextBox.Text;
                    anims.Add(newAnim.Name, newAnim);

                    AnimationsListBox.Items.Add(newAnim.Name);
                    AnimationsListBox.SelectedIndex = AnimationsListBox.Items.Count - 1;

                    AnimationNameTextBox.Text = "";

                    FrameListBox.Items.Clear();
                }
            }
            else
            {
                Help.ShowPopup(this, "Your animation must have a name", new Point(this.Location.X + NewAnimationButton.Location.X, this.Location.Y + NewAnimationButton.Location.Y));
            }
        }

        private void NewFrameButton_Click(object sender, EventArgs e)
        {
            StopPlaying();

            if (AnimationsListBox.SelectedIndex >= 0)
            {
                ancXChange = false;
                ancYChange = false;
                wepXOffset = false;
                wepYOffset = false;

                Frame f = new Frame();

                anims[AnimationsListBox.SelectedItem.ToString()].Frames.Add(f);

                string x = "frame" + (FrameListBox.Items.Count);
                FrameListBox.Items.Add(x);

                FrameListBox.SelectedIndex = FrameListBox.Items.Count - 1;

                rendX = 0;
                rendY = 0;
                rendWidth = 0;
                rendHeight = 0;
                colX = 0;
                colY = 0;
                colWidth = 0;
                colHeight = 0;
                actX = 0;
                actY = 0;
                actWidth = 0;
                actHeight = 0;
                AnchorPointXNumericUpDown.Value = 0;
                AnchorPointYNumericUpDown.Value = 0;
                WeaponOffsetXNumericUpDown.Value = 0;
                WeaponOffsetYNumericUpDown.Value = 0;
                DurationNumericUpDown.Value = (decimal)0.20;
                TriggerTextBox.Text = "";



            }
        }

        private void FrameListBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void AnimationsListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            FrameListBox.ClearSelected();

            FrameListBox.Items.Clear();
            if (AnimationsListBox.SelectedItem != null && anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count > 0)
            {
                for (int i = 0; i < anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count; i++)
                {
                    string x = "frame" + (FrameListBox.Items.Count);
                    FrameListBox.Items.Add(x);
                }
                FrameListBox.SelectedIndex = 0;

            }
            else
                FrameListBox.SelectedIndex = -1;

            //anims[AnimationsListBox.SelectedItem.ToString()].
        }

        private void SpriteSheet_MouseDown(object sender, MouseEventArgs e)
        {
            //if a frame is selected
            if (FrameListBox.SelectedIndex >= 0)
            {
                if (e.Button == MouseButtons.Left)
                {
                    initialMousePosition = e.Location;
                    if (drawActiveRect || drawRendRect || drawColRect)
                    {

                        RectXNumericUpDown.Value = (decimal)initialMousePosition.X - SpriteSheet.AutoScrollPosition.X;
                        RectYNumericUpDown.Value = (decimal)initialMousePosition.Y - SpriteSheet.AutoScrollPosition.Y;



                        RectWidthNumericUpDown.Value = 0;
                        RectHeightNumericUpDown.Value = 0;
                    }
                    else if (placeAnchorPoint)
                    {
                        AnchorPointXNumericUpDown.Value = e.Location.X - SpriteSheet.AutoScrollPosition.X;
                        AnchorPointYNumericUpDown.Value = e.Location.Y - SpriteSheet.AutoScrollPosition.Y;

                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].AnchorPointX = (int)AnchorPointXNumericUpDown.Value;
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].AnchorPointY = (int)AnchorPointYNumericUpDown.Value;
                    }
                    else if (placeWeaponOffset)
                    {
                        WeaponOffsetXNumericUpDown.Value = e.Location.X - SpriteSheet.AutoScrollPosition.X;
                        WeaponOffsetYNumericUpDown.Value = e.Location.Y - SpriteSheet.AutoScrollPosition.Y;

                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].WeaponOffsetX = (int)WeaponOffsetXNumericUpDown.Value;
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].WeaponOffsetY = (int)WeaponOffsetYNumericUpDown.Value;
                    }

                    if (RectInfoComboBox.SelectedIndex == 0)
                    {
                        rendX = (int)RectXNumericUpDown.Value;
                        rendY = (int)RectYNumericUpDown.Value;
                        rendWidth = (int)RectWidthNumericUpDown.Value;
                        rendHeight = (int)RectHeightNumericUpDown.Value;

                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].RendX = rendX;
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].RendY = rendY;
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].RendWidth = rendWidth;
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].RendHeight = rendHeight;
                    }
                    else if (RectInfoComboBox.SelectedIndex == 1)
                    {
                        colX = (int)RectXNumericUpDown.Value;
                        colY = (int)RectYNumericUpDown.Value;
                        colWidth = (int)RectWidthNumericUpDown.Value;
                        colHeight = (int)RectHeightNumericUpDown.Value;

                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ColX = colX;
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ColY = colY;
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ColWidth = colWidth;
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ColHeight = colHeight;
                    }
                    else if (RectInfoComboBox.SelectedIndex == 2)
                    {
                        actX = (int)RectXNumericUpDown.Value;
                        actY = (int)RectYNumericUpDown.Value;
                        actWidth = (int)RectWidthNumericUpDown.Value;
                        actHeight = (int)RectHeightNumericUpDown.Value;

                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ActiveX = actX;
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ActiveY = actY;
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ActiveWidth = actWidth;
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ActiveHeight = actHeight;
                    }
                }
            }
            else
            {
                Help.ShowPopup(this, "You must have a frame selected to edit", new Point(this.Location.X + SpriteSheet.Location.X, this.Location.Y + SpriteSheet.Location.Y));
            }
        }

        private void SpriteSheet_MouseMove(object sender, MouseEventArgs e)
        {
            if (FrameListBox.SelectedIndex >= 0)
            {
                if (e.Button == MouseButtons.Left)
                {
                    currMousePosition = e.Location;

                    if (drawActiveRect || drawRendRect || drawColRect)
                    {
                        if (currMousePosition.X < initialMousePosition.X)
                        {
                            if (currMousePosition.X >= 0)
                            {
                                RectXNumericUpDown.Value = currMousePosition.X - SpriteSheet.AutoScrollPosition.X;
                                RectWidthNumericUpDown.Value = initialMousePosition.X - currMousePosition.X;
                            }
                        }
                        else
                            RectWidthNumericUpDown.Value = currMousePosition.X - initialMousePosition.X;

                        if (currMousePosition.Y < initialMousePosition.Y)
                        {
                            if (currMousePosition.Y >= 0)
                            {
                                RectYNumericUpDown.Value = currMousePosition.Y + SpriteSheet.AutoScrollPosition.Y;
                                RectHeightNumericUpDown.Value = initialMousePosition.Y - currMousePosition.Y;
                            }
                        }
                        else
                            RectHeightNumericUpDown.Value = currMousePosition.Y - initialMousePosition.Y;
                    }
                    else if (placeAnchorPoint && e.Location.X - SpriteSheet.AutoScrollPosition.X >= 0 && e.Location.Y - SpriteSheet.AutoScrollPosition.Y >= 0)
                    {
                        AnchorPointXNumericUpDown.Value = e.Location.X - SpriteSheet.AutoScrollPosition.X;
                        AnchorPointYNumericUpDown.Value = e.Location.Y - SpriteSheet.AutoScrollPosition.Y;

                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].AnchorPointX = (int)AnchorPointXNumericUpDown.Value;
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].AnchorPointY = (int)AnchorPointYNumericUpDown.Value;
                    }
                    else if (placeWeaponOffset && e.Location.X - SpriteSheet.AutoScrollPosition.X >= 0 && e.Location.Y - SpriteSheet.AutoScrollPosition.Y >= 0)
                    {
                        WeaponOffsetXNumericUpDown.Value = e.Location.X - SpriteSheet.AutoScrollPosition.X;
                        WeaponOffsetYNumericUpDown.Value = e.Location.Y - SpriteSheet.AutoScrollPosition.Y;

                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].WeaponOffsetX = (int)WeaponOffsetXNumericUpDown.Value;
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].WeaponOffsetY = (int)WeaponOffsetYNumericUpDown.Value;
                    }

                    if (RectInfoComboBox.SelectedIndex == 0)
                    {
                        //rendX = (int)RectXNumericUpDown.Value;
                        //rendY = (int)RectYNumericUpDown.Value;
                        //rendWidth = (int)RectWidthNumericUpDown.Value;
                        //rendHeight = (int)RectHeightNumericUpDown.Value;
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].RendX = (int)RectXNumericUpDown.Value;
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].RendY = (int)RectYNumericUpDown.Value;
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].RendWidth = (int)RectWidthNumericUpDown.Value;
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].RendHeight = (int)RectHeightNumericUpDown.Value;

                        //RectXNumericUpDown.Value = anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].RendX;
                        //RectYNumericUpDown.Value = anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].RendY;
                        //RectWidthNumericUpDown.Value = anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].RendWidth;
                        //RectHeightNumericUpDown.Value = anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].RendHeight;
                    }
                    else if (RectInfoComboBox.SelectedIndex == 1)
                    {
                        //colX = (int)RectXNumericUpDown.Value;
                        //colY = (int)RectYNumericUpDown.Value;
                        //colWidth = (int)RectWidthNumericUpDown.Value;
                        //colHeight = (int)RectHeightNumericUpDown.Value;
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ColX = (int)RectXNumericUpDown.Value;
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ColY = (int)RectYNumericUpDown.Value;
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ColWidth = (int)RectWidthNumericUpDown.Value;
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ColHeight = (int)RectHeightNumericUpDown.Value;
                    }
                    else if (RectInfoComboBox.SelectedIndex == 2)
                    {
                        //actX = (int)RectXNumericUpDown.Value;
                        //actY = (int)RectYNumericUpDown.Value;
                        //actWidth = (int)RectWidthNumericUpDown.Value;
                        //actHeight = (int)RectHeightNumericUpDown.Value;
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ActiveX = (int)RectXNumericUpDown.Value;
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ActiveY = (int)RectYNumericUpDown.Value;
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ActiveWidth = (int)RectWidthNumericUpDown.Value;
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].ActiveHeight = (int)RectHeightNumericUpDown.Value;
                    }
                }
            }
        }

        private void SpriteSheet_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {

            }
        }

        private void RectInfoComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (RectInfoComboBox.SelectedIndex == 0)
            {
                RectXNumericUpDown.Value = rendX;
                RectYNumericUpDown.Value = rendY;
                RectWidthNumericUpDown.Value = rendWidth;
                RectHeightNumericUpDown.Value = rendHeight;

                drawRendRect = true;
                drawColRect = false;
                drawActiveRect = false;
                placeAnchorPoint = false;
                placeWeaponOffset = false;
            }
            else if (RectInfoComboBox.SelectedIndex == 1)
            {
                RectXNumericUpDown.Value = colX;
                RectYNumericUpDown.Value = colY;
                RectWidthNumericUpDown.Value = colWidth;
                RectHeightNumericUpDown.Value = colHeight;

                drawRendRect = false;
                drawColRect = true;
                drawActiveRect = false;
                placeAnchorPoint = false;
                placeWeaponOffset = false;
            }
            else if (RectInfoComboBox.SelectedIndex == 2)
            {
                RectXNumericUpDown.Value = actX;
                RectYNumericUpDown.Value = actY;
                RectWidthNumericUpDown.Value = actWidth;
                RectHeightNumericUpDown.Value = actHeight;

                drawRendRect = false;
                drawColRect = false;
                drawActiveRect = true;
                placeAnchorPoint = false;
                placeWeaponOffset = false;
            }
        }

        private void AnchorPointXNumericUpDown_ValueChanged(object sender, EventArgs e)
        {

            if (FrameListBox.Items.Count > 0 && AnimationsListBox.SelectedIndex >= 0 && FrameListBox.SelectedIndex >= 0 &&
                            anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count >= 0 &&
                            anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count >= FrameListBox.SelectedIndex + 1)
            {
                if (ancXChange)
                {
                    anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].AnchorPointX = (int)AnchorPointXNumericUpDown.Value;

                }
                ancXChange = true;
            }
        }

        private void AnchorPointYNumericUpDown_ValueChanged(object sender, EventArgs e)
        {

            //check if there is a valid frame selected
            if (FrameListBox.Items.Count > 0 && AnimationsListBox.SelectedIndex >= 0 && FrameListBox.SelectedIndex >= 0 &&
                            anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count >= 0 &&
                            anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count >= FrameListBox.SelectedIndex + 1)
            {
                if (AnimationsListBox.SelectedIndex >= 0 && FrameListBox.SelectedIndex >= 0)
                {
                    if (ancYChange)
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].AnchorPointY = (int)AnchorPointYNumericUpDown.Value;
                    ancYChange = true;
                }
            }
        }

        private void WeaponOffsetXNumericUpDown_ValueChanged(object sender, EventArgs e)
        {

            //check if there is a valid frame selected
            if (FrameListBox.Items.Count > 0 && AnimationsListBox.SelectedIndex >= 0 && FrameListBox.SelectedIndex >= 0 &&
                            anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count >= 0 &&
                            anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count >= FrameListBox.SelectedIndex + 1)
            {
                if (AnimationsListBox.SelectedIndex >= 0 && FrameListBox.SelectedIndex >= 0)
                {
                    if (wepXOffset)
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].WeaponOffsetX = (int)WeaponOffsetXNumericUpDown.Value;
                    wepXOffset = true;
                }
            }
        }

        private void WeaponOffsetYNumericUpDown_ValueChanged(object sender, EventArgs e)
        {

            //check if there is a valid frame selected
            if (FrameListBox.Items.Count > 0 && AnimationsListBox.SelectedIndex >= 0 && FrameListBox.SelectedIndex >= 0 &&
                            anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count >= 0 &&
                            anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count >= FrameListBox.SelectedIndex + 1)
            {
                if (AnimationsListBox.SelectedIndex >= 0 && FrameListBox.SelectedIndex >= 0)
                {
                    if (wepYOffset)
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].WeaponOffsetY = (int)WeaponOffsetYNumericUpDown.Value;
                    wepYOffset = true;
                }
            }
        }

        private void DurationNumericUpDown_ValueChanged(object sender, EventArgs e)
        {

            //check if there is a valid frame selected
            if (FrameListBox.Items.Count > 0 && AnimationsListBox.SelectedIndex >= 0 && FrameListBox.SelectedIndex >= 0 &&
                            anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count >= 0 &&
                            anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count >= FrameListBox.SelectedIndex + 1)
            {
                if (AnimationsListBox.SelectedIndex >= 0 && FrameListBox.SelectedIndex >= 0)
                {
                    if (durationChange)
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].Duration = (double)DurationNumericUpDown.Value;
                    durationChange = true;
                }
            }
        }

        private void TriggerTextBox_TextChanged(object sender, EventArgs e)
        {

            //check if there is a valid frame selected
            if (AnimationsListBox.SelectedIndex >= 0 && FrameListBox.SelectedIndex >= 0 && anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count >= 1 &&
                FrameListBox.SelectedIndex < anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count)
            {
                if (AnimationsListBox.SelectedIndex >= 0 && FrameListBox.SelectedIndex >= 0)
                {
                    if (triggerChange)
                        anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].Trigger = TriggerTextBox.Text;
                    triggerChange = true;
                }
                else
                    TriggerTextBox.Text = "";
            }
        }

        private void TriggerTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                //check if there is a valid frame selected
                if (FrameListBox.Items.Count > 0 && AnimationsListBox.SelectedIndex >= 0 && FrameListBox.SelectedIndex >= 0)
                {
                    if (AnimationsListBox.SelectedIndex >= 0 && FrameListBox.SelectedIndex >= 0)
                    {
                        if (triggerChange)
                            anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].Trigger = TriggerTextBox.Text;
                        triggerChange = true;
                    }
                }
            }
        }

        private void SpriteSheet_Scroll(object sender, ScrollEventArgs e)
        {
            Render();
        }

        private void DeleteAnimationButton_Click(object sender, EventArgs e)
        {
            StopPlaying();

            if (anims.Count > 0)
            {
                anims[AnimationsListBox.SelectedItem.ToString()].Frames.Clear();
                anims.Remove(AnimationsListBox.SelectedItem.ToString());
                string x = AnimationsListBox.SelectedItem.ToString();
                AnimationsListBox.SelectedItem = -1;
                AnimationsListBox.Items.Remove(x);
            }
        }

        private void DeleteFrameButton_Click(object sender, EventArgs e)
        {
            StopPlaying();

            if (FrameListBox.Items.Count > 0)
            {
                for (int i = 0; i < anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count - 1; i++)
                {
                    if (i < FrameListBox.SelectedIndex)
                        continue;
                    //anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].assignment(anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex + 1]);
                    anims[AnimationsListBox.SelectedItem.ToString()].Frames[i].assignment(anims[AnimationsListBox.SelectedItem.ToString()].Frames[i + 1]);
                }
                anims[AnimationsListBox.SelectedItem.ToString()].Frames.RemoveAt(anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count - 1);
                if (FrameListBox.SelectedIndex >= anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count)
                    FrameListBox.SelectedIndex = -1;
                FrameListBox.Items.RemoveAt(anims[AnimationsListBox.SelectedItem.ToString()].Frames.Count);
            }
        }

        private void FrameUpButton_Click(object sender, EventArgs e)
        {
            StopPlaying();

            if (FrameListBox.SelectedIndex > 0)
            {
                Frame f = new Frame();
                f.assignment(anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex]);
                anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].assignment(anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex - 1]);
                anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex - 1].assignment(f);
                FrameListBox.SelectedIndex = FrameListBox.SelectedIndex - 1;
            }
        }

        private void FrameDownButton_Click(object sender, EventArgs e)
        {
            StopPlaying();

            if (FrameListBox.SelectedIndex < FrameListBox.Items.Count - 1)
            {
                Frame f = new Frame();
                f.assignment(anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex]);
                anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex].assignment(anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex + 1]);
                anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex + 1].assignment(f);
                FrameListBox.SelectedIndex = FrameListBox.SelectedIndex + 1;
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AnimationsListBox.Items.Count > 0)
            {
                AnimationsListBox.SelectedIndex = 0;

                SaveFileDialog sfd = new SaveFileDialog();
                //DialogResult dr = sfd.ShowDialog();
                sfd.Filter = "All Files(*.*)|*.*|Animation Files(*.xml)|*.xml";
                sfd.FilterIndex = 2;
                sfd.DefaultExt = "xml";

                sfd.InitialDirectory = System.IO.Path.GetFullPath(System.IO.Directory.GetCurrentDirectory() + "\\..\\MegaLands\\resource\\Animations");

                if (DialogResult.OK == sfd.ShowDialog())
                {
                    XElement pRoot = new XElement("Sheet");
                    XAttribute pRoot2 = new XAttribute("version", "");
                    pRoot.Add(pRoot2);

                    XElement fileName = new XElement("FileName");
                    fileName.Value = animationPath;
                    pRoot.Add(fileName);

                    XElement xanims = new XElement("Animations");

                    for (int i = 0; i < AnimationsListBox.Items.Count; i++)
                    {
                        XElement anim = new XElement("Animation");
                        XAttribute name = new XAttribute("Name", AnimationsListBox.Items[i].ToString());
                        XAttribute loop = new XAttribute("isLooping", 1);

                        anim.Add(name);
                        anim.Add(loop);

                        XElement frames = new XElement("Frames");
                        for (int j = 0; j < anims[AnimationsListBox.Items[i].ToString()].Frames.Count; j++)
                        {//<Frame FrameNumber="0" AnchorPointX="31" AnchorPointY="62" Time="100" Trigger="">
                            XElement xframe = new XElement("Frame");

                            string x = AnimationsListBox.Items[i].ToString();

                            XAttribute fn = new XAttribute("FrameNumber", j);
                            XAttribute anX = new XAttribute("AnchorPointX", anims[AnimationsListBox.Items[i].ToString()].Frames[j].AnchorPointX - anims[AnimationsListBox.Items[i].ToString()].Frames[j].RendX);
                            XAttribute anY = new XAttribute("AnchorPointY", anims[AnimationsListBox.Items[i].ToString()].Frames[j].AnchorPointY - anims[AnimationsListBox.Items[i].ToString()].Frames[j].RendY);
                            XAttribute woX = new XAttribute("WeaponOffsetX", anims[AnimationsListBox.Items[i].ToString()].Frames[j].WeaponOffsetX - anims[AnimationsListBox.Items[i].ToString()].Frames[j].RendX);
                            XAttribute woY = new XAttribute("WeaponOffsetY", anims[AnimationsListBox.Items[i].ToString()].Frames[j].WeaponOffsetY - anims[AnimationsListBox.Items[i].ToString()].Frames[j].RendY);
                            XAttribute dur = new XAttribute("Duration", anims[AnimationsListBox.Items[i].ToString()].Frames[j].Duration);
                            XAttribute trig = new XAttribute("Trigger", anims[AnimationsListBox.Items[i].ToString()].Frames[j].Trigger);

                            xframe.Add(fn);
                            xframe.Add(anX);
                            xframe.Add(anY);
                            xframe.Add(woX);
                            xframe.Add(woY);
                            xframe.Add(dur);
                            xframe.Add(trig);


                            string blah = AnimationsListBox.Items[0].ToString();
                            //<RenderRect x="0" y="0" height="64" width="64"/>
                            //<ActiveCollisionRect x="" y="" height="" width=""/>
                            //<PassiveCollisionRect x="40" y="40" height="64" width="64"/>
                            XElement rend = new XElement("RenderRect");

                            XAttribute rrX = new XAttribute("x", anims[AnimationsListBox.Items[i].ToString()].Frames[j].RendX);
                            XAttribute rrY = new XAttribute("y", anims[AnimationsListBox.Items[i].ToString()].Frames[j].RendY);
                            XAttribute rrW = new XAttribute("width", anims[AnimationsListBox.Items[i].ToString()].Frames[j].RendWidth);
                            XAttribute rrH = new XAttribute("height", anims[AnimationsListBox.Items[i].ToString()].Frames[j].RendHeight);

                            rend.Add(rrX);
                            rend.Add(rrY);
                            rend.Add(rrW);
                            rend.Add(rrH);

                            XElement act = new XElement("ActiveCollisionRect");

                            XAttribute arX = new XAttribute("x", anims[AnimationsListBox.Items[i].ToString()].Frames[j].ActiveX - anims[AnimationsListBox.Items[i].ToString()].Frames[j].RendX);
                            XAttribute arY = new XAttribute("y", anims[AnimationsListBox.Items[i].ToString()].Frames[j].ActiveY - anims[AnimationsListBox.Items[i].ToString()].Frames[j].RendY);
                            XAttribute arW = new XAttribute("width", anims[AnimationsListBox.Items[i].ToString()].Frames[j].ActiveWidth);
                            XAttribute arH = new XAttribute("height", anims[AnimationsListBox.Items[i].ToString()].Frames[j].ActiveHeight);

                            act.Add(arX);
                            act.Add(arY);
                            act.Add(arW);
                            act.Add(arH);

                            XElement col = new XElement("PassiveCollisionRect");

                            XAttribute crX = new XAttribute("x", anims[AnimationsListBox.Items[i].ToString()].Frames[j].ColX - anims[AnimationsListBox.Items[i].ToString()].Frames[j].RendX);
                            XAttribute crY = new XAttribute("y", anims[AnimationsListBox.Items[i].ToString()].Frames[j].ColY - anims[AnimationsListBox.Items[i].ToString()].Frames[j].RendY);
                            XAttribute crW = new XAttribute("width", anims[AnimationsListBox.Items[i].ToString()].Frames[j].ColWidth);
                            XAttribute crH = new XAttribute("height", anims[AnimationsListBox.Items[i].ToString()].Frames[j].ColHeight);

                            col.Add(crX);
                            col.Add(crY);
                            col.Add(crW);
                            col.Add(crH);

                            xframe.Add(rend);
                            xframe.Add(act);
                            xframe.Add(col);

                            frames.Add(xframe);
                        }

                        anim.Add(frames);
                        xanims.Add(anim);
                    }

                    pRoot.Add(xanims);

                    pRoot.Add(anims);
                    pRoot.Save(sfd.FileName);
                }
            }
            else
            {
                Help.ShowPopup(this, "You must have animations to save", new Point(this.Location.X + SpriteSheet.Location.X, this.Location.Y + SpriteSheet.Location.Y));
            }
        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "All Files(*.*)|*.*|Map Files(*.xml)|*.xml";
            ofd.FilterIndex = 2;

            ofd.InitialDirectory = System.IO.Path.GetFullPath(System.IO.Directory.GetCurrentDirectory() + "\\..\\MegaLands\\resource\\Animations");

            if (DialogResult.OK == ofd.ShowDialog())
            {
                if (anims.Count > 0)
                {
                    anims[AnimationsListBox.SelectedItem.ToString()].Frames.Clear();
                    anims.Remove(AnimationsListBox.SelectedItem.ToString());
                    string x = AnimationsListBox.SelectedItem.ToString();
                    AnimationsListBox.SelectedItem = -1;
                    AnimationsListBox.Items.Remove(x);
                    anims.Clear();
                }
                FrameListBox.Items.Clear();
                AnimationsListBox.Items.Clear();


                XElement pRoot = XElement.Load(ofd.FileName);
                XAttribute Ver = pRoot.Attribute("version");

                // <FileName>../../Images/AnchorPointTest.png</FileName>
                XElement xfileNameElement = pRoot.Element("FileName");
                string fileName = xfileNameElement.Value;
                XElement xanims = pRoot.Element("Animations");



                XElement tempAnim;// = xanims.Element("Animation");

                for (int i = 0; i < xanims.Elements().Count<XElement>(); i++)
                {
                    tempAnim = xanims.Elements().ElementAt<XElement>(i);

                    //looping through all of the animations
                    Animation newAnim = new Animation();

                    XAttribute animName = tempAnim.Attribute("Name");

                    newAnim.Name = animName.Value;

                    XAttribute animLooping = tempAnim.Attribute("isLooping");

                    int tempIsLooping = Convert.ToInt32(animLooping.Value);

                    anims.Add(newAnim.Name, newAnim);

                    //add the animation to the animations list box
                    AnimationsListBox.Items.Add(newAnim.Name);
                    AnimationsListBox.SelectedIndex = AnimationsListBox.Items.Count - 1;

                    XElement xframes = tempAnim.Element("Frames");
                    XElement xframe;// = xframes.Element("Frame");


                    //  int j = 0;
                    //loop through the frames of the current animation
                    // foreach (XElement Frame in xframes.Elements())
                    for (int j = 0; j < xframes.Elements().Count<XElement>(); j++)
                    {
                        xframe = xframes.Elements().ElementAt<XElement>(j);
                        Frame newFrame = new Frame();

                        newFrame.AnchorPointX = Convert.ToInt32(xframe.Attribute("AnchorPointX").Value);
                        newFrame.AnchorPointY = Convert.ToInt32(xframe.Attribute("AnchorPointY").Value);

                        newFrame.WeaponOffsetX = Convert.ToInt32(xframe.Attribute("WeaponOffsetX").Value);
                        newFrame.WeaponOffsetY = Convert.ToInt32(xframe.Attribute("WeaponOffsetY").Value);

                        newFrame.Duration = Convert.ToDouble(xframe.Attribute("Duration").Value);

                        newFrame.Trigger = xframe.Attribute("Trigger").Value;

                        XElement xrr = xframe.Element("RenderRect");

                        newFrame.RendX = Convert.ToInt32(xrr.Attribute("x").Value);
                        newFrame.RendY = Convert.ToInt32(xrr.Attribute("y").Value);
                        newFrame.RendWidth = Convert.ToInt32(xrr.Attribute("width").Value);
                        newFrame.RendHeight = Convert.ToInt32(xrr.Attribute("height").Value);


                        XElement xar = xframe.Element("ActiveCollisionRect");

                        newFrame.ActiveX = Convert.ToInt32(xar.Attribute("x").Value);
                        newFrame.ActiveY = Convert.ToInt32(xar.Attribute("y").Value);
                        newFrame.ActiveWidth = Convert.ToInt32(xar.Attribute("width").Value);
                        newFrame.ActiveHeight = Convert.ToInt32(xar.Attribute("height").Value);


                        XElement xcr = xframe.Element("PassiveCollisionRect");
                        newFrame.ColX = Convert.ToInt32(xcr.Attribute("x").Value);
                        newFrame.ColY = Convert.ToInt32(xcr.Attribute("y").Value);
                        newFrame.ColWidth = Convert.ToInt32(xcr.Attribute("width").Value);
                        newFrame.ColHeight = Convert.ToInt32(xcr.Attribute("height").Value);


                        //TODO add the anchor point to the render rect
                        newFrame.AnchorPointX = newFrame.AnchorPointX + newFrame.RendX;
                        newFrame.AnchorPointY = newFrame.AnchorPointY + newFrame.RendY;

                        newFrame.WeaponOffsetX = newFrame.WeaponOffsetX + newFrame.RendX;
                        newFrame.WeaponOffsetY = newFrame.WeaponOffsetY + newFrame.RendY;

                        newFrame.ColX = newFrame.ColX + newFrame.RendX;
                        newFrame.ColY = newFrame.ColY + newFrame.RendY;

                        newFrame.ActiveX = newFrame.ActiveX + newFrame.RendX;
                        newFrame.ActiveY = newFrame.ActiveY + newFrame.RendY;


                        newAnim.Frames.Add(newFrame);

                        string x = "frame" + (FrameListBox.Items.Count);
                        FrameListBox.Items.Add(x);
                    }
                }
                //open the sprite sheet
                imageID = TM.LoadTexture(fullPath + fileName);
                animationPath = fileName;
                //bitSize = new Bitmap("../../Images/" + fileName);
                bitSize = new Bitmap("../MegaLands/resource/Animations/" + fileName);
                Graphics g = SpriteSheet.CreateGraphics();
                bitSize.SetResolution(g.DpiX, g.DpiY);
                g.Dispose();

                SpriteSheet.AutoScrollMinSize = bitSize.Size;

                DX.Resize(SpriteSheet, SpriteSheet.ClientSize.Width, SpriteSheet.ClientSize.Height, true);

                FrameListBox.SelectedIndex = 0;
            }
        }

        private void SpriteSheet_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

            if (files[0].Contains(".png"))
            {
                //load in the sprite sheet
                imageID = TM.LoadTexture(files[0]);
                animationPath = files[0].Substring(files[0].LastIndexOf('\\') + 1);
                //imageID = TM.LoadTexture("TheRoom.png");
                bitSize = new Bitmap(files[0]);
                Graphics g = SpriteSheet.CreateGraphics();
                bitSize.SetResolution(g.DpiX, g.DpiY);
                g.Dispose();

                SpriteSheet.AutoScrollMinSize = bitSize.Size;

                DX.Resize(SpriteSheet, SpriteSheet.ClientSize.Width, SpriteSheet.ClientSize.Height, true);
            }
            else if (files[0].Contains(".xml"))
            {
                //load in the animations file

                if (anims.Count > 0)
                {
                    anims[AnimationsListBox.SelectedItem.ToString()].Frames.Clear();
                    anims.Remove(AnimationsListBox.SelectedItem.ToString());
                    string x = AnimationsListBox.SelectedItem.ToString();
                    AnimationsListBox.SelectedItem = -1;
                    AnimationsListBox.Items.Remove(x);
                    anims.Clear();
                }
                FrameListBox.Items.Clear();
                AnimationsListBox.Items.Clear();


                XElement pRoot = XElement.Load(files[0]);
                XAttribute Ver = pRoot.Attribute("version");

                // <FileName>../../Images/AnchorPointTest.png</FileName>
                XElement xfileNameElement = pRoot.Element("FileName");
                string fileName = xfileNameElement.Value;
                XElement xanims = pRoot.Element("Animations");



                XElement tempAnim;// = xanims.Element("Animation");

                for (int i = 0; i < xanims.Elements().Count<XElement>(); i++)
                {
                    tempAnim = xanims.Elements().ElementAt<XElement>(i);

                    //looping through all of the animations
                    Animation newAnim = new Animation();

                    XAttribute animName = tempAnim.Attribute("Name");

                    newAnim.Name = animName.Value;

                    XAttribute animLooping = tempAnim.Attribute("isLooping");

                    int tempIsLooping = Convert.ToInt32(animLooping.Value);

                    anims.Add(newAnim.Name, newAnim);

                    //add the animation to the animations list box
                    AnimationsListBox.Items.Add(newAnim.Name);
                    AnimationsListBox.SelectedIndex = AnimationsListBox.Items.Count - 1;

                    XElement xframes = tempAnim.Element("Frames");
                    XElement xframe;// = xframes.Element("Frame");


                    //  int j = 0;
                    //loop through the frames of the current animation
                    // foreach (XElement Frame in xframes.Elements())
                    for (int j = 0; j < xframes.Elements().Count<XElement>(); j++)
                    {
                        xframe = xframes.Elements().ElementAt<XElement>(j);
                        Frame newFrame = new Frame();

                        newFrame.AnchorPointX = Convert.ToInt32(xframe.Attribute("AnchorPointX").Value);
                        newFrame.AnchorPointY = Convert.ToInt32(xframe.Attribute("AnchorPointY").Value);

                        newFrame.WeaponOffsetX = Convert.ToInt32(xframe.Attribute("WeaponOffsetX").Value);
                        newFrame.WeaponOffsetY = Convert.ToInt32(xframe.Attribute("WeaponOffsetY").Value);

                        newFrame.Duration = Convert.ToDouble(xframe.Attribute("Duration").Value);

                        newFrame.Trigger = xframe.Attribute("Trigger").Value;

                        XElement xrr = xframe.Element("RenderRect");

                        newFrame.RendX = Convert.ToInt32(xrr.Attribute("x").Value);
                        newFrame.RendY = Convert.ToInt32(xrr.Attribute("y").Value);
                        newFrame.RendWidth = Convert.ToInt32(xrr.Attribute("width").Value);
                        newFrame.RendHeight = Convert.ToInt32(xrr.Attribute("height").Value);


                        XElement xar = xframe.Element("ActiveCollisionRect");

                        newFrame.ActiveX = Convert.ToInt32(xar.Attribute("x").Value);
                        newFrame.ActiveY = Convert.ToInt32(xar.Attribute("y").Value);
                        newFrame.ActiveWidth = Convert.ToInt32(xar.Attribute("width").Value);
                        newFrame.ActiveHeight = Convert.ToInt32(xar.Attribute("height").Value);


                        XElement xcr = xframe.Element("PassiveCollisionRect");
                        newFrame.ColX = Convert.ToInt32(xcr.Attribute("x").Value);
                        newFrame.ColY = Convert.ToInt32(xcr.Attribute("y").Value);
                        newFrame.ColWidth = Convert.ToInt32(xcr.Attribute("width").Value);
                        newFrame.ColHeight = Convert.ToInt32(xcr.Attribute("height").Value);


                        //TODO add the anchor point to the render rect
                        newFrame.AnchorPointX = newFrame.AnchorPointX + newFrame.RendX;
                        newFrame.AnchorPointY = newFrame.AnchorPointY + newFrame.RendY;

                        newFrame.WeaponOffsetX = newFrame.WeaponOffsetX + newFrame.RendX;
                        newFrame.WeaponOffsetY = newFrame.WeaponOffsetY + newFrame.RendY;

                        newFrame.ColX = newFrame.ColX + newFrame.RendX;
                        newFrame.ColY = newFrame.ColY + newFrame.RendY;

                        newFrame.ActiveX = newFrame.ActiveX + newFrame.RendX;
                        newFrame.ActiveY = newFrame.ActiveY + newFrame.RendY;


                        newAnim.Frames.Add(newFrame);

                        string x = "frame" + (FrameListBox.Items.Count);
                        FrameListBox.Items.Add(x);
                    }
                }
                //open the sprite sheet
                imageID = TM.LoadTexture(fullPath + fileName);
                animationPath = fileName;
                //bitSize = new Bitmap("../../Images/" + fileName)  
                bitSize = new Bitmap("../MegaLands/resource/Animations/" + fileName);
                Graphics g = SpriteSheet.CreateGraphics();
                bitSize.SetResolution(g.DpiX, g.DpiY);
                g.Dispose();

                SpriteSheet.AutoScrollMinSize = bitSize.Size;

                DX.Resize(SpriteSheet, SpriteSheet.ClientSize.Width, SpriteSheet.ClientSize.Height, true);

                FrameListBox.SelectedIndex = 0;
                //end loading


            }
        }

        private void SpriteSheet_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Copy;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //anims[AnimationsListBox.SelectedIndex.ToString()].Frames[FrameListBox.SelectedIndex];
            Frame newFrame = new Frame();

            newFrame.assignment(anims[AnimationsListBox.SelectedItem.ToString()].Frames[FrameListBox.SelectedIndex]);

            anims[AnimationsListBox.SelectedItem.ToString()].Frames.Add(newFrame);

            string x = "frame" + (FrameListBox.Items.Count);
            FrameListBox.Items.Add(x);

            FrameListBox.SelectedIndex = FrameListBox.Items.Count - 1;
        }

        private void ButtonColor()
        {
            if (drawRendRect)
            {
                RenderRectButton.BackColor = Color.Yellow;
                CollisionRectButton.BackColor = Button.DefaultBackColor;
                ActiveRectButton.BackColor = Button.DefaultBackColor;
                AnchorPointButton.BackColor = Button.DefaultBackColor;
                WeaponOffsetButton.BackColor = Button.DefaultBackColor;
            }
            if (drawColRect)
            {
                RenderRectButton.BackColor = Button.DefaultBackColor;
                CollisionRectButton.BackColor = Color.Yellow; ;
                ActiveRectButton.BackColor = Button.DefaultBackColor;
                AnchorPointButton.BackColor = Button.DefaultBackColor;
                WeaponOffsetButton.BackColor = Button.DefaultBackColor;
            }
            if (drawActiveRect)
            {
                RenderRectButton.BackColor = Button.DefaultBackColor;
                CollisionRectButton.BackColor = Button.DefaultBackColor;
                ActiveRectButton.BackColor = Color.Yellow; ;
                AnchorPointButton.BackColor = Button.DefaultBackColor;
                WeaponOffsetButton.BackColor = Button.DefaultBackColor;
            }
            if (placeAnchorPoint)
            {
                RenderRectButton.BackColor = Button.DefaultBackColor;
                CollisionRectButton.BackColor = Button.DefaultBackColor;
                ActiveRectButton.BackColor = Button.DefaultBackColor;
                AnchorPointButton.BackColor = Color.Yellow; ;
                WeaponOffsetButton.BackColor = Button.DefaultBackColor;
            }
            if (placeWeaponOffset)
            {
                RenderRectButton.BackColor = Button.DefaultBackColor;
                CollisionRectButton.BackColor = Button.DefaultBackColor;
                ActiveRectButton.BackColor = Button.DefaultBackColor;
                AnchorPointButton.BackColor = Button.DefaultBackColor;
                WeaponOffsetButton.BackColor = Color.Yellow; ;
            }
        }

        private void SpriteSheet_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            ancXChange = ancYChange = true;
            AnchorPointXNumericUpDown.Value = (rendX + (rendX + rendWidth)) / 2;
            AnchorPointYNumericUpDown.Value = (rendY + (rendY + rendHeight)) / 2;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ancYChange = true;
            AnchorPointYNumericUpDown.Value = rendY + rendHeight;
        }
    }
}
